//
//  DistributionManagement.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/27.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "DistributionManagement.h"
#import "ADDReceivingWay.h"
@interface DistributionManagement ()<UITableViewDataSource,UITableViewDelegate> {
    UITableView *_tableView;
    NSMutableArray *_dataSource;
    NSMutableArray *deleteArr;
}

@end

@implementation DistributionManagement

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"配送方式";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(releaseBtnAction:)];
    [self allocTableView];
    [self LoadDataSource];
}
#pragma mark --初始化tableView--
- (void)allocTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:_tableView];
}
#pragma mark --加载数据--
- (void)LoadDataSource {
    _dataSource = [NSMutableArray array];
    [_dataSource addObject:@"顺丰"];
    [_dataSource addObject:@"德邦"];
    [_dataSource addObject:@"韵达"];
}
#pragma mark --编辑事件-- 
- (void)releaseBtnAction:(UIBarButtonItem *)sender {
    if ([sender.title isEqual:@"编辑"]) {
        sender.title = @"删除";
//        _tableView.allowsSelectionDuringEditing = YES;//进入删除状态
        _tableView.allowsMultipleSelectionDuringEditing = YES; //进入多选删除状态
        [_tableView setEditing:YES animated:YES]; //将tableview设置为可编辑
        
    }else {
        sender.title = @"编辑";
        _tableView.allowsMultipleSelectionDuringEditing = NO; //取消多选删除状态
        [_tableView setEditing:NO animated:YES]; //将tableview设置为不可编辑状态
        if (!deleteArr.count) {
            return;
        }
        //选中的行
        NSArray *selectedRows = [NSArray arrayWithArray:deleteArr];
        NSMutableIndexSet *indicesOfItemsToDelete = [NSMutableIndexSet new];
        for (NSIndexPath *selectionIndex in selectedRows) {
            [indicesOfItemsToDelete addIndex:selectionIndex.row];
        }
        //从数据源中删除所选行对应的值
        [_dataSource removeObjectsAtIndexes:indicesOfItemsToDelete];
        
        //删除所选的行
        [_tableView deleteRowsAtIndexPaths:selectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

#pragma mark -
#pragma mark --TableViewDelegate--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
        {
            return 1;
        }
        default:
            return _dataSource.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = @"添加";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else {
        cell.textLabel.text = _dataSource[indexPath.row];
        if ([tableView.indexPathsForSelectedRows containsObject:indexPath]) {
            cell.selected = YES;
        }else {
            cell.selected = NO;
        }
    }
    
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10.0;
}
//选中方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        [self pushViewController:[ADDReceivingWay new]];
        return;
    }
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"编辑"]) {
        return;
    }
    [self updateDeleteData];
    
}
//取消选中方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return;
    }
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"编辑"]) {
        return;
    }
    [self updateDeleteData];
}


#pragma mark - 更新选中cell数组--
-(void)updateDeleteData
{
    NSArray *selectedRows = [_tableView indexPathsForSelectedRows];
    
    [deleteArr removeAllObjects];
    deleteArr = nil;
    deleteArr = [NSMutableArray arrayWithArray:selectedRows];
    
    
}

#pragma mark ---deit delete---
// 让 UITableView 和 UIViewController 变成可编辑状态

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    [_tableView setEditing:editing animated:animated];
}


// 指定哪一行可以编辑 哪行不能编辑
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return NO;
    }else {
       return YES;
    }
    
}
// 设置 哪一行的编辑按钮 状态 指定编辑样式
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}
// 判断点击按钮的样式 来去做添加 或删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //单选删除添加
    /*
    // 删除的操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_dataSource removeObjectAtIndex:indexPath.row];
        NSArray *indexPaths = @[indexPath]; // 构建 索引处的行数 的数组
        // 删除 索引的方法 后面是动画样式
        [_tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationLeft)];
        
    }
    
    // 添加的操作
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        
//        NSArray *indexPaths = @[indexPath];
//        [_tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationRight)];
        
    }
     */
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
